package com.mwy.springbootrabbitmq.mq.topic.send;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * 主题模式--生产者
 */
public class Send {

    //声明交换机
    private final static String EXCHANGE_NAME = "exchange_topic";

    public static void main(String[] args) throws Exception {
        //创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/mwy");

        try(
                Connection connection = factory.newConnection();
                Channel channel = connection.createChannel()
        ){
            //声明交换机
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
            String message1 = "主题队列消息1";
            String message2 = "主题队列消息2";
            String message3 = "主题队列消息3";
            String routingKey1 = "quick.orange.rabbit";
            String routingKey2 = "lazy.pink.rabbit";
            String routingKey3 = "quick.orange.male.rabbit";
            //发送消息
            channel.basicPublish(EXCHANGE_NAME,routingKey1,null,message1.getBytes("UTF-8"));
            channel.basicPublish(EXCHANGE_NAME,routingKey2,null,message2.getBytes("UTF-8"));
            channel.basicPublish(EXCHANGE_NAME,routingKey3,null,message3.getBytes("UTF-8"));
            System.out.println("生产者发送消息："+message1);
            System.out.println("生产者发送消息："+message2);
            System.out.println("生产者发送消息："+message3);
        }
    }
}
